<?php

namespace App\Admin\Controllers;

use App\Models\Traffic;
use App\Models\Trafficline;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class TrafficlineController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('交通路线管理')
            ->description('')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('交通路线详情')
            ->description('')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑交通路线')
            ->description('')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增交通路线')
            ->description('')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Trafficline);

        $grid->id('ID');
        $grid->traffic_id()->badge();
        $grid->coverimage()->image('http://media.weijingguanjia.com/',36,36);
        $grid->columns('starttime','endtime','startadd','endadd','trafficlineid');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Trafficline::findOrFail($id));

        $show->id('ID');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Trafficline);
        $form->select('traffic_id','所属交通')->options(Traffic::all()->pluck('title','id'));
        $form->image('coverimage','交通路线图像')->uniqueName()->removable()->rules('required');
        $form->text('starttime','起始时间')->rules('required');
        $form->text('endtime','到达时间')->rules('required');
        $form->text('startadd','起始位置')->rules('required');
        $form->text('endadd','到达位置')->rules('required');
        $form->text('trafficlineid','交通路线编号')->rules('required');
        return $form;
    }
}
